/*****************************************************************************************
   
   Copyright (c) Microsoft Corporation. All rights reserved.

   Use of this code sample is subject to the terms of the Microsoft
   Public License, a copy of which should always be distributed with
   this file.  You can also access a copy of this license agreement at:
   http://www.microsoft.com/opensource/licenses.mspx#Ms-PL

 ****************************************************************************************/
using System;

namespace Microsoft.CSA.FeedSync.FeedSyncLibrary
{
    public class XmlNamespaceManagerHelper
    {
        static private System.Collections.Generic.SortedList<string, System.Xml.XmlNamespaceManager> s_XmlNamespaceManagerList = new System.Collections.Generic.SortedList<string, System.Xml.XmlNamespaceManager>();

        static public string InitializeXmlNamespaceManager(System.Xml.XmlDocument i_XmlDocument, System.Xml.XmlNamespaceManager i_XmlNamespaceManager, string i_PreferredNamespacePrefix, string i_NamespaceURI)
        {
            string NamespacePrefix = String.Empty;

            if (s_XmlNamespaceManagerList.ContainsValue(i_XmlNamespaceManager))
            {
                NamespacePrefix = i_XmlNamespaceManager.LookupPrefix(i_NamespaceURI);

                if (!System.String.IsNullOrEmpty(NamespacePrefix))
                {
                    return NamespacePrefix;
                }
            }

            #region Populate namespaces into XmlNamespaceManager instance

            System.Xml.XmlReader XmlReader = new System.Xml.XmlNodeReader(i_XmlDocument);

            while (XmlReader.Read())
            {
                XmlReader.MoveToFirstAttribute();
                do
                {
                    if (XmlReader.Name.StartsWith("xmlns:") || (XmlReader.Name == "xmlns"))
                    {
                        NamespacePrefix = System.String.Empty;
                        string NamespaceValue = System.String.Empty;

                        if (XmlReader.Name.StartsWith("xmlns:"))
                            NamespacePrefix = XmlReader.Name.Substring(XmlReader.Name.IndexOf(':') + 1);

                        NamespaceValue = XmlReader.Value;

                        if (!System.String.IsNullOrEmpty(NamespaceValue))
                            i_XmlNamespaceManager.AddNamespace(NamespacePrefix, NamespaceValue);
                    }
                }
                while (XmlReader.MoveToNextAttribute());
            }

            XmlReader.Close();

            #endregion

            #region Get/set namespace prefix for subsequent xpath queries

            foreach (System.Collections.Generic.KeyValuePair<string, string> KeyValuePair in i_XmlNamespaceManager.GetNamespacesInScope(System.Xml.XmlNamespaceScope.All))
            {
                if (KeyValuePair.Value != i_NamespaceURI)
                    continue;

                if (KeyValuePair.Key == i_PreferredNamespacePrefix)
                {
                    NamespacePrefix = i_PreferredNamespacePrefix;
                    break;
                }

                //  Check for default namespace
                if (System.String.IsNullOrEmpty(KeyValuePair.Key))
                {
                    //  See if FeedSync prefix used
                    if (!i_XmlNamespaceManager.HasNamespace(i_PreferredNamespacePrefix))
                    {
                        NamespacePrefix = i_PreferredNamespacePrefix;
                    }
                    else
                    {
                        //  See if FeedSync prefix is mapped
                        string NamespaceURI = i_XmlNamespaceManager.LookupNamespace(i_PreferredNamespacePrefix);
                        if (NamespaceURI == i_NamespaceURI)
                            NamespacePrefix = i_PreferredNamespacePrefix;
                        else
                        {
                            //  Create a new prefix
                            NamespacePrefix = i_PreferredNamespacePrefix + System.Guid.NewGuid().ToString();
                        }
                    }

                    //  Add namespace with mapped prefix
                    i_XmlNamespaceManager.AddNamespace
                        (
                        NamespacePrefix,
                        i_NamespaceURI
                        );

                    break;
                }
            }

            if (System.String.IsNullOrEmpty(NamespacePrefix))
            {
                i_XmlNamespaceManager.AddNamespace
                    (
                    i_PreferredNamespacePrefix,
                    i_NamespaceURI
                    );

                NamespacePrefix = i_PreferredNamespacePrefix;
            }

            s_XmlNamespaceManagerList[NamespacePrefix] = i_XmlNamespaceManager;

            return NamespacePrefix;

            #endregion
        }
    }
}
